//
//  HMNewsViewController.m
//  支付宝项目
//
//  Created by mac on 17/1/3.
//  Copyright © 2017年 mac.gu. All rights reserved.
//

#import "HMNewsViewController.h"
#import "Masonry.h"
@interface HMNewsViewController ()<UITableViewDelegate,UITableViewDataSource>

//记录编辑按钮状态
@property (nonatomic, assign) BOOL isSelected;

//圆圈按钮状态
@property (nonatomic, assign) BOOL isCycleSelected;

@end

static NSString *cellID = @"cellID";

@implementation HMNewsViewController{
    
    UIView *_editView;
    UIButton *_editButton;
    UILabel *_countLable;
    UITableView *_tableView;
    UIButton *_cyclebutton;
    
}

#define kArrayCount 10

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    //加载编辑view
    [self craetEditView];
    
    [self creatTableView];
    
    
    
    
}

//创建编辑视图
- (void)craetEditView{
    
    _editView = [[UIView alloc]init];
    _editView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_editView];
    [_editView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.right.left.equalTo(self.view);
        make.height.offset(45);
    }];
    
    // 创建显示的条数Lable
    _countLable = [[UILabel alloc]init];
    _countLable.text = @"共0条";
    [_editView addSubview:_countLable];
#warning lable
    [_countLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_editView);
        make.left.offset(15);
    }];
    //创建编辑按钮
    
    _editButton = [[UIButton alloc]init];
    [self.view addSubview:_editButton];
    [_editButton setTitle:@"批量管理" forState:UIControlStateNormal];
    [_editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(-20);
        make.centerY.equalTo(_editView);
    }];
    
    [_editButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
// 创建tableView

- (void)creatTableView{
    _tableView = [[UITableView alloc] init];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_editView.mas_bottom);
        make.left.right.equalTo(_editView);
        make.bottom.equalTo(self.view);
    }];
    
    
}

// 批量管理点击事件
- (void)btnClick:(UIButton *)sender{
    //
    
    
    //    _tableView.editing = _tableView.editing;
    
    if (_isSelected == YES) {
        
        //运行编辑
        _tableView.editing = NO;
        _tableView.allowsMultipleSelectionDuringEditing = NO;
        
        //移除cycleBtn
        [_cyclebutton removeFromSuperview];
        
        _isSelected = NO;
        [_editButton setTitle:@"批量管理" forState:UIControlStateNormal];
        _countLable.text = @"共0条";
        
        
        //        [_countLable mas_updateConstraints:^(MASConstraintMaker *make) {
        //           make.left.offset(15);
        //        }];
        
        
    } else {
        _tableView.editing = YES;
        _tableView.allowsMultipleSelectionDuringEditing = YES;
        _isSelected = YES;
        [_editButton setTitle:@"取消" forState:UIControlStateNormal];
        
        //创建全选圆形按钮
        _cyclebutton = [[UIButton alloc]init];
        [_cyclebutton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
        [_editView addSubview:_cyclebutton];
        
        [_cyclebutton mas_makeConstraints:^(MASConstraintMaker *make) {
            //            make.left.offset(10);
            make.left.equalTo(_editView).offset(10);
            make.centerY.equalTo(_editView);
        }];
        
        [_cyclebutton addTarget:self action:@selector(cycleBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        _countLable.text = @"全选";
        
#warning label
        [_countLable mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.offset(50);
        }];
        
        
        
        [self check];
        
    }
    
    
    
}

//全选按钮点击事件
- (void)cycleBtnClick{
    
    
    
    if (_isCycleSelected == YES) {
        [_cyclebutton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
        [self check];
        _isCycleSelected = NO;
    }else{
        [_cyclebutton setImage:[UIImage imageNamed:@"checkSelected"] forState:UIControlStateNormal];
        [self checkSelectd];
        _isCycleSelected = YES;
    }
    
    
}



#pragma mark <UITableDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return kArrayCount;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.textLabel.text = @(indexPath.row).description;
    
    cell.selectedBackgroundView = [[UIView alloc] init];
    
    
    return cell;
}

#pragma mark <UITableViewDelegate>
//选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //正常情况跳转播放界面
    
    //改变编辑图片
    if (_editButton.selected == YES) {
        
        [_cyclebutton setImage:[UIImage imageNamed:@"checkSelected"] forState:UIControlStateNormal];
        _editButton.selected = YES;
        
    }
    //    else{
    //
    //        [_cyclebutton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    //        _editButton.selected = NO;
    //    }
    
    
    
    
}



//设置选中图片
- (void)checkSelectd{
    for (int i = 0; i < kArrayCount; i++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        
        
        UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
        for (UIView *subView in cell.subviews) {
            if ([subView isKindOfClass: NSClassFromString(@"UITableViewCellEditControl")]) {
                
                
                
                UIImageView *imageView = (UIImageView *)[subView valueForKey:@"_imageView"];
                
                
                imageView.image = [UIImage imageNamed:@"checkSelected"];
                
            }
        }
        
        
    }
}

//设置非选中图片
- (void)check{
    
    for (int i = 0; i < kArrayCount; i++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        
        
        UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
        for (UIView *subView in cell.subviews) {
            if ([subView isKindOfClass: NSClassFromString(@"UITableViewCellEditControl")]) {
                
                UIImageView *imageView = (UIImageView *)[subView valueForKey:@"_imageView"];
                
                imageView.image = [UIImage imageNamed:@"check"];
                
            }
        }
    }
}

@end
